import { IsString } from 'class-validator';
import { createUUID } from 'src/utils/tools';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Users } from './users.entity';
import { Auth } from './auth.entity';

@Entity({ comment: '用户角色表' })
export class Roles {
  constructor() {
    this.id = createUUID();
  }

  @PrimaryColumn('varchar', { length: 32 })
  id: string;

  @Column({ length: 50, comment: '名字' })
  @IsString()
  name: string;

  @Column({ comment: '描述' })
  @IsString()
  description: string;

  @CreateDateColumn({ name: 'created_at', comment: '创建时间' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', comment: '更新时间' })
  updatedAt: Date;

  @ManyToMany(() => Users, (user) => user.roles)
  users: Users[];

  @ManyToMany(() => Auth, (auth) => auth.roles)
  @JoinTable({ name: 'roles_auth' })
  auth: Auth[];
}
